<?php
namespace app\admin\controller;

use app\BaseController;
use app\common\facade\admin\AdminAdmin;
use think\exception\ValidateException;

class Admin extends BaseController
{
    public function index($name='Think Php') {
        return 'Hello '.$name; 
    }

    public function adminhello($name='hello') {
        return 'admin模块 admin/adminhello 参数：'.$name;
    }

    /**
     * 管理员列表
     *  */
    public function adminList()
    {
        $param = $this->request->get([
            'query',
            'offset' => $this->request->get('pagenum/d')?:0,
            'length' => $this->request->get('pagesize/d')?:20,
            'ordername' => $this->request->get('ordername')?: 'id',
            'order' => $this->request->get('order')?:'descending'
        ]);

        try {
            $adminList = AdminAdmin::adminList($param);
        } catch(ValidateException $e) {
            return validateJson($e->getError());
        } catch(\Exception $e) {
            return exceptionJson($e->getMessage(), $e->getCode()); 
        }

        return successJson($adminList);
    }

    /**
     * 用户状态更新
     */
    public function upadminState($name=null, $enable=2){        
        $param = [
            'enable' => $enable
        ];

        try{
            $data = AdminAdmin::updateEnable($name,$param);
        } catch (\Exception $e) {
            return exceptionJson($e->getMessage(), $e->getCode());
        }

        return successJson($data);
    }

    /**
     * 新增用户
     */
    public function addAdminUser()
    {
        $param = $this->request->post([
            'username',
            'truename',
            'password',
            'mobile',
            'email',
            'channel',
            'depart',
            'roleId'
        ]);
        // var_dump($param);
        try {            
            $this->validate($param, 'app\common\validate\Admin.addsave');            
            $data = AdminAdmin::adminAddEditSave($param);            
        } catch (ValidateException $e) {
            return validateJson($e->getError());
        } catch (\Exception $e) {
            return exceptionJson($e->getMessage(), $e->getCode());
        }

        return successJson($data);
    }

    /**
     * 编辑用户信息提交
     */
    public function editadminInfo($name=null)
    {
        if(empty($name)) exception('用户id为空');

        $param = $this->request->put([
            'id' => $name,
            'truename',
            'password',
            'roleId',
            'mobile',
            'email',
            'head_img',
            'depart',
            'channel'
        ]);
        
        try {            
            $this->validate($param, 'app\common\validate\Admin.editsave');            
            $data = AdminAdmin::adminAddEditSave($param);            
        } catch (ValidateException $e) {
            return validateJson($e->getError());
        } catch (\Exception $e) {
            return exceptionJson($e->getMessage(), $e->getCode());
        }

        return successJson($data);
    }

    /**
     * 编辑用户个人信息
     */
    public function editSelfInfo($name=null)
    {
        if(empty($name)) exception('用户id为空');

        $param = $this->request->put([
            'id' => $name,
            'truename',
            'mobile',
            'email',
        ]);

        try {            
            $this->validate($param, 'app\common\validate\Admin.SelfSave');            
            $data = AdminAdmin::adminAddEditSave($param);      
        } catch (ValidateException $e) {
            return validateJson($e->getError());
        } catch (\Exception $e) {
            return exceptionJson($e->getMessage(), $e->getCode());
        }

        return successJson($data);
    }

    /**
     * 保存个人头像
     */
    public function upSelfHeadImg($name=null)
    {
        if(empty($name)) exception('用户id为空');

        $param = $this->request->put([
            'id' => $name,
            'head_img',
        ]);

        try {                    
            $data = AdminAdmin::adminAddEditSave($param);      
        } catch (ValidateException $e) {
            return validateJson($e->getError());
        } catch (\Exception $e) {
            return exceptionJson($e->getMessage(), $e->getCode());
        }

        return successJson($data);
    }

    /**
     * 修改用户密码
     */
    public function useruppass($name=null) {
        if(empty($name)) exception('用户id为空');
        
        $param = $this->request->put([
            'id' => $name,
            'oldpass',
            'newpass',
            'rplpass'
        ]);

        try {
            $data = AdminAdmin::useruppass($param);   
        } catch (\Exception $e) {
            return exceptionJson($e->getMessage(), $e->getCode());
        }

        return successJson($data);
    }

    /**
     * 编辑用户角色信息
     */
    public function editAdminRoles()
    {
        $param = $this->request->post([
            'id',
            'roleId'
        ]);

        try {                     
            $data = AdminAdmin::adminAddEditSave($param);            
        } catch (ValidateException $e) {
            return validateJson($e->getError());
        } catch (\Exception $e) {
            return exceptionJson($e->getMessage(), $e->getCode());
        }

        return successJson($data);
    }

    /**
     * 管理员删除
     */
    public function adminDelete($id = NULL)
    {
        $param = [
            'id' => $id
        ];

        try {                    
            $data = AdminAdmin::adminDelete($param);            
        } catch (\Exception $e) {
            return exceptionJson($e->getMessage(), $e->getCode());
        }

        return successJson($data);
    }

    /**
     * 管理员批量删除
     */
    public function adminDeletes()
    {
        $param = $this->request->post([
            'idstr'
        ]);

        try {                    
            $data = AdminAdmin::adminDeletes($param);            
        } catch (\Exception $e) {
            return exceptionJson($e->getMessage(), $e->getCode());
        }

        return successJson($data);
    }

    /**
     * 根据用户ID，查询用户信息
     */
    public function adminInfo($id = NULL)
    {
        $param = [
            'id' => $id
        ];

        try {
            $data = AdminAdmin::adminInfo($param);
        } catch (\Exception $e) {
            return exceptionJson($e->getMessage(), $e->getCode());
        }

        return successJson($data);
    }

    /**
     * 权限列表
     */

    public function rullAdminList()
    {
        try {
            $data = AdminAdmin::rullAdminList();
        } catch (ValidateException $e){
            return validateJson($e->getError());
        } catch (\Exception $e){
            return exceptionJson($e->getMessage(), $e->getCode());
        }

        return successJson($data);
    }

}
